/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package mapping;
import java.sql.ResultSet;
import entreprise.Patient;
import java.sql.SQLException;
import java.util.List;
import mapping.SqlLib;
/**
 *
 * @author Quoc-Khai
 */
public class DBPatient {
    public Patient getPatient(int patientID) throws SQLException{
         Patient iPatient = new Patient();
        String selectQuery;
        selectQuery = "SELECT * FROM patient WHERE patientID='"+patientID+"'";
        
        SqlLib connec = new SqlLib();
        if (connec.initConnection()) {
            ResultSet sqlResultSet = connec.getDataFromQuery(selectQuery);
            
            while(sqlResultSet.next()) {
               iPatient.setPatientID(sqlResultSet.getString("patientID")); 
               iPatient.setPatientName(sqlResultSet.getString("patientName")); 
               iPatient.setPatientSex(sqlResultSet.getString("patientSex")); 
               iPatient.setPatientDateOfBirth(sqlResultSet.getDate("patientDateOfBirth"));
               iPatient.setPatientJob(sqlResultSet.getString("patientJob")); 
               iPatient.setPatientAddress(sqlResultSet.getString("patientAddress")); 
               iPatient.setPatientPhoneNumber(sqlResultSet.getString("patientPhoneNumber")); 
               iPatient.setPatientEmail(sqlResultSet.getString("patientEmail")); 
               iPatient.setPatientHeight(sqlResultSet.getFloat("patientHeight")); 
               iPatient.setPatientWeight(sqlResultSet.getFloat("patientWeight")); 
               iPatient.setPatientBloodType(sqlResultSet.getString("patientBloodType"));  
            }
        } else {
            System.out.println("Patient not found!");
        } 
        //TODO;
        return iPatient;
    }
    public boolean createPatient(Patient patient){
        //TODO;
        java.sql.Date sqlDate = new java.sql.Date(patient.getPatientDateOfBirth().getTime());
        String insertQuery;
        insertQuery = "INSERT INTO patient (patientName, patientSex, patientDateOfBirth, patientJob, patientAddress, patientPhoneNumber, patientEmail, patientHeight, patientWeight, patientBloodType)"
                + "VALUES "
                + "('" + 
                patient.getPatientName() + "','" + 
                patient.getPatientSex()+ "','" + 
                sqlDate + "','" + 
                patient.getPatientJob()+ "','" + 
                patient.getPatientAddress()+ "','" + 
                patient.getPatientPhoneNumber()+ "','" + 
                patient.getPatientEmail()+ "','" + 
                patient.getPatientHeight()+ "','" + 
                patient.getPatientWeight()+ "','" + 
                patient.getPatientBloodType() + 
                "')";
        SqlLib connec = new SqlLib();
        
        if (connec.initConnection()) {
            connec.execQuery(insertQuery);
            System.out.println("Insert Patient success!");
            return true;
        } 
        return false;
    }
    public boolean updatePatient(Patient patient){
        //TODO
        java.sql.Date sqlDate = new java.sql.Date(patient.getPatientDateOfBirth().getTime());
        String updateQuery;
        updateQuery = "UPDATE patient SET "
                + "patientName='" + patient.getPatientName()
                + "', patientSex='" + patient.getPatientSex()
                + "',patientDateOfBirth='" + sqlDate
                + "', patientJob='" + patient.getPatientJob()
                + "', patientAddress='" + patient.getPatientAddress()
                + "', patientPhoneNumber='" + patient.getPatientPhoneNumber()
                + "', patientEmail='" + patient.getPatientEmail()
                + "', patientHeight='" + patient.getPatientHeight()
                + "', patientWeight='" + patient.getPatientWeight()
                + "', patientBloodType='" + patient.getPatientBloodType()
                + "' WHERE patientID='"+patient.getPatientID()+"'";
        
        SqlLib connec = new SqlLib();
        
        if (connec.initConnection()) {
            connec.execQuery(updateQuery);
            System.out.println("Update Patient success!");
            return true;
        }
        return false;
    }
    public boolean deletePatient(int patientID){
        //TODO
        String deleteQuery;
        deleteQuery = "DELETE FROM patient WHERE patientID='"+patientID+"'"; 
        SqlLib connec = new SqlLib();
        
        if (connec.initConnection()) {
            connec.execQuery(deleteQuery);
            System.out.println("Patient Drug success!");
            return true;
        } 
        return false;
    }    
    public List<Patient> getPatients(String conditions){
        List<Patient> patients=null;
        //TODO
        return patients;
    }
}
